package com.gouwudai.gobbs.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IAfficheDAO;
import com.gouwudai.gobbs.pojo.TbbsAffiche;
import com.litt.core.exception.DBException;
/**
 * 
 * <pre>
 *  Title:公告信息DAO
 *  Description: 公告信息DAO  
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @date 2007-3-29
 * @version 1.0
 * <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容: 
 * </pre>
 */
public class AfficheDAOImpl extends BaseDAOImpl implements IAfficheDAO 
{
	private static final Logger logger = Logger.getLogger(AfficheDAOImpl.class);	
	private static final String BUSI_NAME = "公告信息";
	
    /**
     * 保存公告信息
     * @param tbbsAffiche 公告信息对象
     * @return Integer 新增公告信息的ID
     */
	public Integer save(TbbsAffiche tbbsAffiche)
	{    		
		return super.saveObject(tbbsAffiche);		
	}
	
	public TbbsAffiche load(Integer id)
	{
		return (TbbsAffiche)super.loadObject(TbbsAffiche.class, id);
	} 
	
    public void update(TbbsAffiche tbbsAffiche)
    {
    	StringBuilder hql = new StringBuilder();
        hql.append("update TbbsAffiche set classId=?"); 
        hql.append(", title=?");
        hql.append(", content=?");
        hql.append(", validDays=?");
        hql.append(" where affId=?");
        super.updateObject(hql.toString(), new Object[]{
        	tbbsAffiche.getClassId(),
        	tbbsAffiche.getTitle(),
        	tbbsAffiche.getContent(),
        	tbbsAffiche.getValidDays(),
        	tbbsAffiche.getAffId()});
    }
    
    public void delete(Integer affId)
    {
        super.deleteObject(TbbsAffiche.class.getName(),"affId",affId);
    }
    
    /**
     * 删除多个
     * @param affIds
     * @throws DBException
     */
    public void delete(String affIds)
    {
        super.deleteObjects(TbbsAffiche.class.getName(),"affId",affIds);
    }
    
    /**
     * 查询当前分类/版面的公告信息，前8项
     * @param classId
     * @return
     * @throws DBException
     */
    public List listByClassId(Integer classId)
    {
    	String hql = "from TbbsAffiche where classId="+classId+" or classId=-1 order by affId desc";
    	return super.listByHql(hql,1,8);
    } 
    
    public List list(int pageIndex,int pageSize)
    {
    	String hql = "from TbbsAffiche order by affId desc";
    	return super.listByHql(hql,pageIndex,pageSize);
    }        

    /**
     * 首页和全部版面的公告只能由系统管理员增加
     * @param pageIndex
     * @param pageSize
     * @param sort
     * @return
     * @throws DBException
     */
    public IPageList listPage(int pageIndex,int pageSize,String sort)
    {       
    	StringBuilder listSql = new StringBuilder("SELECT A.*,B.CLASS_NAME FROM TbbsAffiche A,TbbsClass B WHERE A.CLASS_ID>0 AND A.CLASS_ID=B.CLASS_ID ");
    	listSql.append(" UNION ALL SELECT *,'首页' FROM TbbsAffiche WHERE CLASS_ID=0 UNION ALL SELECT *,'全部版面' FROM TbbsAffiche WHERE CLASS_ID=-1");
    	if(sort!=null)
    	    listSql.append(sort);
    	return super.listSqlPage(listSql.toString(),pageIndex,pageSize);
    }
    
    /**
     * 根据分类/版面查询公告
     * @param pageIndex
     * @param pageSize
     * @param sort
     * @return
     * @throws DBException
     */
    public IPageList listPageByClassId(int classId,int pageIndex,int pageSize,String sort)
    {       
    	StringBuilder listSql = new StringBuilder("SELECT * FROM TbbsAffiche WHERE CLASS_ID=");    	
    	listSql.append(classId);
    	listSql.append(sort);
    	return super.listSqlPage(listSql.toString(),pageIndex,pageSize);
    }    
    
}
